<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <style type="text/css">
        body, html{width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
        #allmap {
            width:100%;
            height:50%;
        }
        #resultContainer{
            width:100%;
            height:50%;
        }
        #resultContainer button{
            margin:15px;
        }
        #r-result{
            margin:10px;
            font-size:14px;
            color:#333;
            height:60%;
            overflow:scroll; 
            border-radius:5px;
            border:1px solid #666;
            padding:5px;
        }

    </style>
    <script type="text/javascript"
            src="https://api.map.baidu.com/api?v=2.0&ak=my8OeWiwATya00e1N8GdNalXxG6GCsLN&s=1"></script>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <title>浏览器定位</title>
</head>
<body>
<div id="allmap"></div>
<div id="resultContainer">
    <button onclick="getPosition()">开始定位车辆</button>
    <!--    <button onclick="stopPosition()">停止定位</button>-->
    <!--    <button onclick="watchPosition()">连续定位</button>-->
    <button onclick="cleanResult()">清空日志</button>
    <button onclick="sendPosition()">发送定位</button>
    <h3><label>请输入你的车牌号：</label><input type="text" name="carid" id="carid" width="200px;"></h3>
    <h3><label>定位经度：</label><input type="text" name="longitude" id="longitude" width="200px;"></h3>
    <h3><label>定位纬度：</label><input type="text" name="latitude" id="latitude" width="200px;"></h3>
    <div id="r-result">

    </div>
</div>
</body>
</html>
<script type="text/javascript">
    var map = new BMap.Map("allmap");
    var point = new BMap.Point(116.331398,39.897445);
    map.centerAndZoom(point,12);
    map.enableScrollWheelZoom();
    var geolocation = new BMap.Geolocation();
    var result= '',timer1;
    // 开启SDK辅助定位
    geolocation.enableSDKLocation();

    // 开始定位
    function getPosition(){
        map.clearOverlays();
        geolocation.getCurrentPosition(function(r){
            if(this.getStatus() == BMAP_STATUS_SUCCESS){
			var	longitude =r.longitude;//经度获取
			var	latitude =r.latitude;//纬度获取
                var mk = new BMap.Marker(r.point);
                map.addOverlay(mk);
                map.panTo(r.point);
			   $("#longitude").val(longitude);
			    $("#latitude").val(latitude);
            }
            else {
                document.getElementById('r-result').innerHTML = '获取定位结果失败！';
            }
            }, {
            enableHighAccuracy: true,
            timeout: 5000,
            maximumAge: 3000,
            SDKLocation:true
        })
    }

    // 设置周期回掉函数
    function watchPosition(){

        timer1 =  setInterval(getPosition, 3000);

    }

    // 清空定时器
    function stopPosition() {
        if(timer1) {
            window.clearInterval(timer1)
        }
    }

    // 清空定位结果
    function cleanResult() {
        document.getElementById('r-result').innerHTML = '';
		$("#longitude").val("");
		 $("#latitude").val("");
        $("#carid").val("");
    }

	//发送定位
	function sendPosition(){
	 var carid = document.getElementById("carid");
	 var longitude = document.getElementById("longitude");
	 var latitude = document.getElementById("latitude");
	  var text = carid.value;
	  var text2 = longitude.value;
	    var text3 = latitude.value;
	    var textValue = text.replace(/(^\s*)|(\s*$)/g, "");     //替换输入内容当中所有的空字符，包括全角空格，半
	    var textValue2 = text2.replace(/(^\s*)|(\s*$)/g, "");
	    var textValue3 = text3.replace(/(^\s*)|(\s*$)/g, "");
	    if(textValue==null || textValue=="") {
	        alert("请输入车牌号");
	        return false;
	    }else if(textValue2==null||textValue3==null||textValue2==""||textValue3==""){
			alert("请检查定位是否正常");
			return false;
		}else{
			//构建参数
			var obj = {
				 "carid":text,
				 "longitude":text2,
				 "latitude":text3,

			}
			$.ajax({
				type : "POST",
				url : 'http://192.168.1.2:8080/TransportManage_war_exploded/mapnum/addMapNum',
				data : obj, //直接传对象参数
				dataType : 'json',
				success : function(data) {
					 alert("发送成功"+data.msg);
				},
				error:function(){
					 alert("发送失败"+data.msg);
				}
			});

		}


	}
</script>